import cateList from '../../dataJson/categroy.json';
import {api, http} from '../../http';

const fetchFn = async category_name => {
  return await http.get(api.CATE_FILTER, {category_name});
};
export default {
  namespace: 'categroy',
  state: {
    navCategories: [],
    category_tree: [],
    filters: [],
    sort_types: [],
    default_sort_type: '',
  },
  // action 返回一个Promise
  effects: {
    *CATEGORY_LIST({payload: category_name}, {put, call, select}) {
      const res = yield call(fetchFn, category_name); // 异步函数
      // console.log(data.data, 'setPro');
      yield put({type: 'setCateList', res});
      return 'todo'; // 返回Promise 数据
    },
  },
  reducers: {
    setNavCategories(data, res) {
      data.navCategories = res.payload;
      return data; // 必须返回state
    },
    setCateList(data, {res}) {
      console.log(res, 'res');
      data.sort_types = res.sort_types || [];
      data.filters = res.filters || [];
      data.category_tree = res.category_tree || [];
      data.default_sort_type = res.default_sort_type || '';
      return data; // 必须返回state
    },
  },
  subscriptions: {
    init({dispatch, history}) {
      // ss
      dispatch({
        type: 'setNavCategories',
        payload: cateList,
      });
    },
  },
};
